<?php
Class Login_model extends CI_Model {

	function Login_model()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function insert($username, $kode_info_cabang, $kode_member)
	{
		$password = rand(100000,999999);
		$data = array(
		   'username' => $username,
		   'password' => $password,
		   'kode_privilege' => '3',
		   'kode_info_cabang' => $kode_info_cabang,
		   'kode_member' => $kode_member
		);

		$this -> db -> insert('login', $data);

		if ($this -> db -> affected_rows() > 0) {
			return $password;
		}
		else {
			return false;
		}
	}

	function login($username, $password)
	{
		$this -> db -> select('username, password, kode_privilege, kode_info_cabang, login.kode_member, kode_unit, kode_upline, active');
		$this -> db -> from('login');
		$this -> db -> join('member', 'login.kode_member = member.kode_member', 'left');
		$this -> db -> where('username', $username);
		$this -> db -> where('password', $password);
		$this -> db -> limit(1);

		$query = $this -> db -> get();

		if($query -> num_rows() == 1)
		{
			return $query->result();
		}
		else
		{
			return false;
		}
	}

	function cabang($kode_privilege, $kode_info_cabang)
	{
		$this -> db -> select('cabang.kode_cabang, nama_cabang, database_cabang, kode_harga');
		$this -> db -> from('cabang');
		if($kode_privilege != '0')
		{
			$this -> db -> join('info_cabang', 'info_cabang.kode_cabang = cabang.kode_cabang', 'inner');
			$this -> db -> where('kode_info_cabang', $kode_info_cabang);
		}

		$query = $this -> db -> get();

		if($query -> num_rows() > 0)
		{
			return $query->result();
		}
		else
		{
			return false;
		}
	}
}
?>